"use strict";
/* Controllers */
app.controller("dataMapAddController", ['$scope', '$location', '$rootScope', '$routeParams', 'dataMapAddService', function ($scope, $location, $rootScope, $routeParams, dataMapAddService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.dim = {};
    vm.result = {};
    vm.p.depName = 'HRG';

    vm.p.type = $routeParams.type;
    if (vm.p.type == 'id') {
        vm.p.id = $routeParams.id;
    } else {
        vm.p.topicTbl = $routeParams.id;
    }

    var errorFn = function (result) {
        alert(result);
    };

//    $scope.syncDpMeta = function () {
//        var vm = $scope.vm;
//        var topicTbl = vm.p.topicTbl;
//        if (topicTbl == '' || typeof (topicTbl) == 'undefined') {
//            alert('请输入表名称');
//            return;
//        }
//        var params = {
//            topicTbl: topicTbl
//        }
//        var procFn = function (response) {
//            if (response.success && response.data) {
//                var data = response.data;
//                if (data == null) {
//                    return;
//                }
//                vm.p.topicTblId = data.topicTblId;
//                vm.p.topicTblDesc = data.topicTblDesc;
//                vm.p.sourceRelation = data.sourceRelation;
//                vm.p.ownerId = data.ownerId;
//            } else {
//                alert(response.msg);
//            }
//        };
//        return dataMapAddService.syncDpMeta(params, procFn, errorFn);
//    }
    $scope.getMetricSource = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        // 请求参数
        var params = {
            id: vm.p.id,
            topicTbl: vm.p.topicTbl
        };

        // 结果处理
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            vm.p = data;
            var sourceRelationFmt = function (list) {
                var ret = [];
                for (var i in list) {
                    var it = list[i];
                    if (it.relationType == '上游依赖') {
                        ret.push(it.topicTbl);
                    }
                }
                return ret.join(',');
            };
            vm.p.sourceRelation = sourceRelationFmt(data.relationEntityList);

            vm.p.topicGroupEx= findNode(vm.dim.dimTree, vm.p.topicGroup);
            if(typeof(vm.p.topicGroupEx)!='undefined') {
                vm.dim.selectTopicNodes = vm.p.topicGroupEx.nodes;
                vm.p.topicEx= findNode(vm.dim.selectTopicNodes, vm.p.topic);
            }

        };
        return dataMapAddService.getMetricSource(params, procFn, errorFn);
    };

    $scope.setMetricSource = function () {
        var vm = $scope.vm;

        vm.p.topicGroup = vm.p.topicGroupEx.text;
        vm.p.topicGroupEn = readTopicGroupEn(vm.p.topicGroup);
        vm.p.topic = vm.p.topicEx.text;

        var params = vm.p;
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("执行成功！");
            } else {
                alert(response.msg);
            }
        };
        return dataMapAddService.setMetricSource(params, procFn, errorFn);
    };

    $scope.searchDimDefine = function () {
        var vm = $scope.vm;
        var params = {
            typeCode: '主题划分'
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                vm.dim.data = response.data;
                vm.dim.dimTree = dim2Tree(vm.dim.data);
            } else {
                alert(response.msg);
            }
        };
        return dataMapAddService.searchDimDefine(params, procFn, errorFn);
    };

    $scope.selectTopicGroupChange = function() {
        var vm = $scope.vm;

        var item = findNode(vm.dim.dimTree, vm.p.topicGroupEx.text);
        if(item!=null && typeof(item)!='undefined') {
             vm.dim.selectTopicNodes = item.nodes;
        }
    }
    function findNode(list, text) {
        var item = list.find(function (it) {
            return it.text == text
        });
        return  item;
    }


    $scope.initializeController = function () {
        vm.result.ms = '';

        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageItem: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        if(vm.p.id!='0') {
            $scope.searchDimDefine().then(function(){
                $scope.getMetricSource();
            });
        }else {
            $scope.searchDimDefine();
        }
    };

    $scope.initializeController();

}]);


/* Services */
app.service('dataMapAddService', ['ajaxService', function (ajaxService) {

    this.getMetricSource = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/data_map/getMetricSource.do", successFunction, errorFunction);
    };

    this.setMetricSource = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/data_map/setMetricSource.do", successFunction, errorFunction);
    };

//    this.syncDpMeta = function (params, successFunction, errorFunction) {
//        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/data_map/syncDpMeta.do", successFunction, errorFunction);
//    };

    this.searchDimDefine = function (dataMetrics, successFunction, errorFunction) {
        return ajaxService.AjaxPost(dataMetrics, "/star/api/basic-service/v1/dim_define/searchDimDefine.do", successFunction, errorFunction);
    };

}]);


